<template>
    <view>
        <template v-for="(item, index) in list">
            <view :key="index" class="chat-list-item" @tap="goChat(item)">
                <min-badge :count="item.userCount">
                    <!-- app上面不显示，所以写死 -->
                    <u-image :src="getAvatarForShop(item)" width="80rpx" height="80rpx" border-radius="10rpx"></u-image>
                </min-badge>
                <view style="margin-left: 20rpx; width: 83%">
                    <view style="display: flex; margin-top: 5rpx">
                        <view style="font-size: 28rpx; color: #000; width: 55%; text-overflow: ellipsis; overflow: hidden; white-space: nowrap">
                            {{ item.shopName || item.storeName || '官方客服' }}
                        </view>
                        <view style="font-size: 24rpx; margin-top: 8rpx; width: 270rpx; text-align: right; float: right; color: #b3b3b3">{{ item.createTime }}</view>
                        <!-- <view style="font-size: 24rpx;margin-top: 8rpx;width: 270rpx;text-align: right;float: right;color: #B3B3B3;">{{item.contentTime}}</view> -->
                    </view>
                    <view v-if="item.content" style="margin-top: 10rpx; font-size: 24rpx; text-overflow: ellipsis; overflow: hidden; white-space: nowrap">
                        <u-icon v-if="/http/.test(item.content)" size="38" name="photo"></u-icon>
                        <text v-else>{{ item.content }}</text>
                    </view>
                    <view :style="item.content ? 'margin-top: 20rpx;height: 1rpx;background-color: #d8d8d8;' : 'margin-top: 40rpx;height: 1rpx;background-color: #d8d8d8;'"></view>
                </view>
            </view>
        </template>
        <u-empty v-if="list.length === 0" text="数据为空" mode="data"></u-empty>
    </view>
</template>

<script>
import minBadge from '@/components/min-badge/min-badge';
import defaultHead from '@/static/tenant/logo.png';
export default {
    comments: { minBadge },
    data() {
        return {
            list: [],
            defaultHead
        };
    },
    onShow() {
        this.getList('');
    },
    methods: {
        getAvatarForShop(item) {
			console.log('111',item.shopLogo);
			console.log('222',item.storeHead);
			console.log('333',this.defaultHead);
            return item.shopLogo || item.storeHead || this.defaultHead;
        },
        getDateDiff(data) {
            // 传进来的data必须是日期格式，不能是时间戳
            //var str = data;
            //将字符串转换成时间格式
            var timePublish = new Date(data);
            var timeNow = new Date();
            var minute = 1000 * 60;
            var hour = minute * 60;
            var day = hour * 24;
            var month = day * 30;
            var result = '2';

            var diffValue = timeNow - timePublish;
            var diffMonth = diffValue / month;
            var diffWeek = diffValue / (7 * day);
            var diffDay = diffValue / day;
            var diffHour = diffValue / hour;
            var diffMinute = diffValue / minute;

            if (diffMonth > 3) {
                result = timePublish.getFullYear() + '-';
                result += timePublish.getMonth() + '-';
                result += timePublish.getDate();
            } else if (diffMonth > 1) {
                //月
                result = data.substring(0, 10);
            } else if (diffWeek > 1) {
                //周
                result = data.substring(0, 10);
            } else if (diffDay > 1) {
                //天
                result = data.substring(0, 10);
            } else if (diffHour > 1) {
                //小时
                result = parseInt(diffHour) + this.content1;
            } else if (diffMinute > 1) {
                //分钟
                result = parseInt(diffMinute) + this.content2;
            } else {
                result = this.content3;
            }
            return result;
        },
        goChat(item) {
            uni.navigateTo({
                url: '/pages/chat/chat?storeId=' + item.storeId + '&shopId=' + item.shopId
            });
        },
        getList(type) {
            let userId = uni.getStorageSync('uid');
            if (!userId) {
                this.$goLogin();
            }
            this.$request(this.$baseHost + '/mall-business/chat/userList', { userId, storeId: uni.getStorageSync('tenantId') }, 'get').then((res) => {
                if (res.code === 200) {
                    this.list = res.result.map((d) => {
                        d.contentTime = this.getDateDiff(d.contentTime);
                        return d;
                    });
                }
                if (type === 'Refresh') {
                    uni.stopPullDownRefresh(); // 停止刷新
                }
            });
        }
    }
};
</script>

<style lang="less" scoped>
.u-empty {
    margin-top: 60% !important;
}
.chat-list-item {
    display: flex;
    background: #fff;
    height: max-content;
    padding: 20rpx 10rpx 20rpx 30rpx;
}
</style>
